package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.Info.AllTypePageInfo;
import com.tracesystem.controller.form.*;
import com.tracesystem.model.IssueState;
import com.tracesystem.model.IssueType;
import com.tracesystem.dao.IssueTypeDao;
import com.tracesystem.service.IssueTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssueTypeException;
import com.tracesystem.util.exception.code.IssueTypeExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueTypeServiceImpl extends ServiceImpl<IssueTypeDao, IssueType> implements IssueTypeService {

    /**
     * 缺陷实现层对象
     */
    @Resource
    private IssueTypeDao issueTypeDao;

    /**
     * 缺陷分类业务层对象
     */
    @Resource
    private IssueTypeService issueTypeService;


    /**
     * 修改缺陷分类状态
     * @param renewalTypeModuleStateForm
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateModuleState(RenewalTypeModuleStateForm renewalTypeModuleStateForm) {
        IssueType issueType = issueTypeService.getOne(//查询类型是否存在
                new QueryWrapper<IssueType>().eq("TYPE_CODE", renewalTypeModuleStateForm.getTypeCode()));
        System.out.println(issueType.getModuleState()+"-------------");
        if (issueType.getModuleState().equals("有效")){
            issueType.setModuleState("禁用");
        }else {
            issueType.setModuleState("有效");
        }
        int i = issueTypeDao.updateById(WoniuCopyUtil.copyOne(issueType, IssueType.class));
        if(i>0) return Result.getInstance().setMsg("修改缺陷状态成功");
        return Result.getInstance().setCode(50001).setMsg("修改缺陷状态失败");
    }

    /**
     * 修改缺陷分类
     * @param renewalTypeDescForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateTypeDesc(RenewalTypeDescForm renewalTypeDescForm) {
        IssueType issueType = issueTypeService.getOne(//查询类型是否存在
                new QueryWrapper<IssueType>().eq("TYPE_CODE", renewalTypeDescForm.getTypeCode()));
        if(null==issueType) {
            throw new IssueTypeException(IssueTypeExceptionCode.IssueTYPE_NOTEXIST_ERROR);
        }else {
            if(issueType.getTypeDesc().equals(renewalTypeDescForm.getTypeDesc())){
                throw new IssueTypeException(IssueTypeExceptionCode.IssueTYPE_EXIST_ERROR);
            }else {
                boolean b = issueTypeService.saveOrUpdate(WoniuCopyUtil.copyOne(renewalTypeDescForm, IssueType.class));
                if(!b) return Result.getInstance().setMsg("缺陷编号不存在,已经为您新增该缺陷分类");
                return Result.getInstance().setMsg("修改缺陷分类成功");
            }
        }
    }

    /**
     * 根据模糊条件查询缺陷分类
     * @param likeTypeListForm
     * @return
     */
    @Override
    public Result findPageTypeListByTypeDesc(LikeTypeListForm likeTypeListForm) {
        QueryWrapper<IssueType> wrapper = new QueryWrapper<IssueType>().orderByDesc("TYPE_CODE");
        if(likeTypeListForm.getTypeDesc() != null&& !likeTypeListForm.getTypeDesc().trim().equals(""))
            wrapper.like("TYPE_DESC",likeTypeListForm.getTypeDesc());

        Page<IssueType> wa = page(new Page<>(likeTypeListForm.getPageIndex(),likeTypeListForm.getPageSize()),wrapper);
        List<AllTypePageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllTypePageInfo.class);

        Page<AllTypePageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    /**
     * 查询全部缺陷分类
     * @param findAllTypeListForm
     * @return
     */
    @Override
    public Result getAllTypeList(FindAllTypeListForm findAllTypeListForm) {
        QueryWrapper<IssueType> wrapper = new QueryWrapper<IssueType>().orderByDesc("TYPE_CODE");

        Page<IssueType> wa = page(new Page<>(findAllTypeListForm.getPageIndex(),findAllTypeListForm.getPageSize()),wrapper);
        List<AllTypePageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllTypePageInfo.class);

        Page<AllTypePageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    //添加
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> insertType(AddTypeForm addTypeForm) {
        IssueType issueType = issueTypeService.getOne(//查询类型是否存在
                new QueryWrapper<IssueType>().eq("TYPE_DESC", addTypeForm.getTypeDesc()));
        //存在则抛出已存在异常
        if(null!=issueType) throw new IssueTypeException(IssueTypeExceptionCode.IssueTYPE_EXIST_ERROR);
//        addTypeForm.setModuleState("0");//默认值为0
        issueTypeService.save(WoniuCopyUtil.copyOne(addTypeForm,IssueType.class));
        return Result.getInstance().setMsg("新增缺陷分类成功");
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectType(RemoveTypeForm removeTypeForm) {
        boolean b = issueTypeService.removeById(removeTypeForm.getTypeCode());
        if(!b) throw new IssueTypeException(IssueTypeExceptionCode.IssueTYPE_NOTEXIST_ERROR);
        return Result.getInstance().setMsg("删除缺陷分类成功");
    }

    //批量删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectTypeList(RemoveTypeListForm removeTypeListForm) {
        List<Integer> typeCodes = removeTypeListForm.getTypeCodes();
        for (Integer typeCode : typeCodes) {
            boolean b = issueTypeService.removeById(typeCode);
            if(!b) throw new IssueTypeException(IssueTypeExceptionCode.IssueTYPE_NOTEXIST_ERROR);
            continue;
        }
        return Result.getInstance().setMsg("删除缺陷分类成功");
    }
}
